﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DataAccess;
using System.Data;

namespace Bussiness
{
    public class CourseManager
    {
        private int week;
        private int courseTime;
        public CourseManager()
        {
            week = Convert.ToInt32(DateTime.Now.DayOfWeek);
            DateTime currentTime = DateTime.Now;
            //week = 5;
            //DateTime currentTime = Convert.ToDateTime("15:00");
            courseTime = ConvertDayTimeToCourseTime(currentTime);
        }
        public SortedList GetCurrentClassRooms()
        {
            SortedList crList = new SortedList(); 
            CourseData cd = new CourseData();
            DataTable dt = cd.GetCurrentClassRooms(week, courseTime);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                crList.Add(dt.Rows[i]["CRcode"], dt.Rows[i]["StuCount"]);               
            }
            return crList;            
        }
        public SortedList GetCurrentCrWithID()
        {
            SortedList crList = new SortedList();
            CourseData cd = new CourseData();
            DataTable dt = cd.GetCurrentClassRooms(week, courseTime);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                crList.Add(dt.Rows[i]["CRcode"], dt.Rows[i]["CourseID"]);
            }
            return crList;
        }
        public DataTable GetCurrentCrByCode(int crCode)
        {
            CourseData cd = new CourseData();
            return cd.GetCurrentCrByCode(week,courseTime,crCode);
        }

        public int GetStuCountByCourseID(int courseID)
        {
            CourseData cd = new CourseData();
            return cd.GetStuCountByCourseID(courseID);
        }

        //***************************Private Method Below*****************************
        private int ConvertDayTimeToCourseTime(DateTime dayTime)
        {
            int courseTime=-1;
            //DateTime currentTime = DateTime.Now;
            DateTime currentTime = dayTime;
            //上午
            DateTime c1 = Convert.ToDateTime("08:20");
            DateTime c2 = Convert.ToDateTime("10:20");
            DateTime c3 = Convert.ToDateTime("12:00");
            //下午
            DateTime c4 = Convert.ToDateTime("14:00");
            DateTime c5 = Convert.ToDateTime("15:50");
            DateTime c6 = Convert.ToDateTime("17:30");
            //晚上
            DateTime c7 = Convert.ToDateTime("18:30");
            DateTime c8 = Convert.ToDateTime("20:20");
            DateTime c9 = Convert.ToDateTime("22:00");


            if (DateTime.Compare(currentTime, c1) > 0 && DateTime.Compare(currentTime, c2) < 0)
            {
                courseTime = 12;
            }
            else if (DateTime.Compare(currentTime, c2) > 0 && DateTime.Compare(currentTime, c3) < 0)
            {
                courseTime = 34;
            }
            else if (DateTime.Compare(currentTime, c4) > 0 && DateTime.Compare(currentTime, c5) < 0)
            {
                courseTime = 56;
            }
            else if (DateTime.Compare(currentTime, c5) > 0 && DateTime.Compare(currentTime, c6) < 0)
            {
                courseTime = 78;
            }
            else if (DateTime.Compare(currentTime, c7) > 0 && DateTime.Compare(currentTime, c8) < 0)
            {
                courseTime = 910;
            }
            else if (DateTime.Compare(currentTime, c8) > 0 && DateTime.Compare(currentTime, c9) < 0)
            {
                courseTime = 1112;
            }
            else
            {
                courseTime = -1;
            }
            return courseTime; 
        }
    }
}
